package com.ext.springcloud.controller;

import com.ext.springcloud.entity.Account;
import com.ext.springcloud.result.CommonResult;
import com.ext.springcloud.service.AccountService;
import com.ext.springcloud.utils.JwtUtil;
import com.ext.springcloud.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
@RestController
@RefreshScope
public class AccountController {

    @Resource
    private AccountService accountService;
    @Resource
    private RedisUtils redisUtils;

    @RequestMapping("/account/decrease")
    public CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money) {
        accountService.decrease(userId, money);
        return new CommonResult(200, "扣减账户余额成功！");
    }

    @RequestMapping("/account/setAccountInfo")
    public CommonResult setAccountInfo(@RequestBody Account account) {
        accountService.setAccountInfo(account);
        return new CommonResult(200, "success");
    }

    @RequestMapping("/login")
    public CommonResult login(@RequestParam("userId") Long userId) {
        log.info("登录用户Id:{}",userId);
        //todo 从数据库验证 后续更改
        Account account = new Account();
        account.setId(userId);
        account.setHeadImg("image");
        account.setName("user"+userId);
        String token = JwtUtil.geneJsonWebToken(account);

//        log.info("token:{}",token);
//        log.info("===============start=================");
//        redisUtils.set("tokenInfo",token);
//        log.info("redisGetToken:{}",redisUtils.get("tokenInfo"));
//        log.info("===============end=================");

        return new CommonResult(200, "loginSuccess", token);
    }
}
